﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessObjects.BO_GetObjects;
using BussinessObjects;
using BusinessObjects;
using LibraryO2S;
using System.IO;

public partial class controls_admin_ReferentNewsEdit : Pages_Base
{
    private DataTable objDT;
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            try
            {
                loadSetting();
                loadData();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
    private void loadData()
    {
        if (Request["a_id"] == null)
            return;
        DataTable objDT = BO_Articles.Search(Convert.ToInt32(Request["a_id"]));
        if (objDT != null && objDT.Rows.Count > 0)
        {
            txtFULL_ARTICLE_TITLE.Text = objDT.Rows[0]["FULL_ARTICLE_TITLE"].ToString();
            txtLinks.Text = objDT.Rows[0]["FULL_ARTICLE_TITLE_ENG"].ToString();
            divImg.InnerHtml = "<img src='../upload/articles/" + objDT.Rows[0]["THUMB_IMG"].ToString() + "' height = '100' alt='" + objDT.Rows[0]["FULL_ARTICLE_TITLE"].ToString() + "' title='" + objDT.Rows[0]["FULL_ARTICLE_TITLE"].ToString() + "' />";
        }
        else
            divMessage.InnerHtml = O2S_Message.Warning("Hệ Thống", "Bản ghi này không tồn tại hoặc đã bị xóa.");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            int a_id = Convert.ToInt32(Request["a_id"]);
            objDT = BO_Articles.Search(a_id);
            string filename = objDT.Rows[0]["THUMB_IMG"].ToString();
            try
            {
                if (fupImage.HasFile)
                {
                    filename = fupImage.FileName;

                    int count = 0;
                    while (File.Exists(Server.MapPath("~/upload/articles/") + filename))
                    {
                        filename = count + "_" + fupImage.FileName;
                        count++;
                    }
                    fupImage.SaveAs(Server.MapPath("~/upload/articles/") + filename);
                }
            }
            catch (Exception ex)
            {
                divMessage.InnerHtml = O2S_Message.Error("Hệ Thống", "Lỗi ghi file! " + ex.Message);
                return;
            }
            int c_id = 2;// Convert.ToInt32(ddlCategories.SelectedValue);
            int status = 1;
            string title = txtFULL_ARTICLE_TITLE.Text.Trim();
            string full_article_description = "";// txtFULL_ARTICLE_DESCRIPTION.Text.Trim();
            string links = txtLinks.Text.Trim();
            bool displayOnHome = true;// cboDisplay.Checked;
            DataTable admin = (DataTable)Session["Admin"];
            //string c_id = "";//";-1:" + sort_display + ";";
            string m_key = "";// txtMetaKeyword.Text.Trim();
            string m_desc = "";// txtDescription.Text.Trim();
            string strResult = BO_Articles.Update(a_id, c_id, "",
                    2, "", filename, "", "", title, links, "", "", full_article_description, "", "", "", "", m_key, m_desc, 0,
                    0, 0, 0, displayOnHome, "",
                    Convert.ToDateTime(objDT.Rows[0]["CREATED_DATE"]), objDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, admin.Rows[0]["USER_NAME"].ToString(), status);
            if (strResult != "0")
                divMessage.InnerHtml = O2S_Message.Success("Thông Báo", "Cập nhật thành công!");
            else
                divMessage.InnerHtml = O2S_Message.Error("Lỗi", "Có lỗi trong quá trình xử lý!");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally { base.CloseConnection(); }
    }
    private void loadSetting()
    {
        DataTable objDT = BO_Settings.SelectByGroupId(3, 1);
        foreach (DataRow dr in objDT.Rows)
        {
            string value = dr["SETTING_VALUE"].ToString();
            switch (dr["SETTING_KEY"].ToString())
            {
                case "NOTE_LINKS":
                    NOTE_LINKS.InnerText = value;
                    break;
                //case "NOTE_DISPLAY":
                //    NOTE_DISPLAY.InnerText = value;
                //    break;
                //case "NOTE_MODULE":
                //    NOTE_MODULE.InnerText = value;
                //    break;
                //case "NOTE_METAKEYWORD":
                //    NOTE_METAKEYWORD.InnerText = value;
                //    break;
                //case "NOTE_TITLE":
                //    NOTE_TITLE.InnerText = value;
                //    break;
                //case "NOTE_DESCRIPT":
                //    NOTE_DESCRIPT.InnerText = value;
                //    break;
                //case "NOTE_URL":
                //    NOTE_URL.InnerText = value;
                //    break;
            }
        }
    }
}
